import { createRouter, createWebHistory } from 'vue-router'
import { ElNotification } from 'element-plus'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      // 登录界面
      path: '/login',
      name: 'login',
      component: () => import('../views/login/index.vue')
    },
    {
      // 注册界面
      path: '/register',
      name: 'register',
      component: () => import('../views/register/index.vue')
    },
    {
      // 发现音乐
      path: '/findmusic',
      name: 'findmusic',
      children: [
        {
          // 个性推荐
          path: '/findmusic/recommend',
          name: 'Recommend',
          component: () => import('../views/main/FindMusic/Recommend/index.vue')
        },
        {
          // 歌单
          path: '/findmusic/playlist',
          name: 'PlayList',
          component: () => import('../views/main/FindMusic/PlayList/index.vue')
        },
        {
          // 主播电台
          path: '/findmusic/Map',
          name: 'map',
          component: () => import('../views/main/FindMusic/Map/index.vue')
        },
        {
          // 歌手
          path: '/findmusic/singer',
          name: 'Singer',
          component: () => import('../views/main/FindMusic/Singer/index.vue')
        },
        {
          // 最新音乐
          path: '/findmusic/latestmusic',
          name: 'LatestMusic',
          component: () => import('../views/main/FindMusic/LatestMusic/index.vue')
        }
      ]
    },
    {
      // 播放音乐
      path: '/musicplayer',
      name: 'MusicPlayer',
      component: () => import('../views/main/MusicPlayer/index.vue')
    },
    {
      // 教学体化
      path: '/teaching',
      name: 'teaching',
      children: [
        {
          // 通用管理
          path: '/teaching/general',
          name: 'General',
          component: () => import('../views/main/TeachingSysten/General/index.vue')
        },
        {
          // 流程管理
          path: '/teaching/process',
          name: 'Process',
          component: () => import('../views/main/TeachingSysten/Process/index.vue')
        },
        {
          // 数据管理
          path: '/teaching/information',
          name: 'Information',
          component: () => import('../views/main/TeachingSysten/Information/index.vue')
        }
      ]
    },
    {
      // 我的视频
      path: '/customer',
      name: 'Customer',
      component: () => import('../views/main/CustomerSystem/index.vue')
    },
    {
      // 办公系统
      path: '/office',
      name: 'Office',
      component: () => import('../views/main/OfficeSystem/index.vue')
    },
    {
      // 办公系统
      path: '/customerinfo',
      name: 'Customerinfo',
      component: () => import('../views/main/CustomerInfo/index.vue')
    },
    {
      // 个人设置
      path: '/user',
      name: 'user',
      children: [
        {
          // 个人主页
          path: '/user/homepage',
          name: 'homepage',
          component: () => import('../views/main/User/HomePage/index.vue')
        },
        {
          // 账号设置
          path: '/user/setting',
          name: 'setting',
          component: () => import('../views/main/User/Setting/index.vue')
        }
      ]
    },
    {
      // 测试
      path: '/test',
      name: 'test',
      component: () => import('../views/test/index.vue')
    }
  ]
})

router.beforeEach((to, from, next) => {
  const info = JSON.parse(sessionStorage.getItem('userState'))
// document.cookie('userState', JSON.stringify(userState), { expires: new Date(Date.now() + 24 * 60 * 60 * 1000) })

  // 处理登录页面的情况
  if (to.path === '/login' || to.path === '/register' || to.path === '/test') {
    next()
    return // 确保不再继续执行后续逻辑
  }

  if (!info || !info.username || !info.password) {
    // 阻止导航
    next('/login')
    // 使用 Element Plus 的通知
    ElNotification({
      title: '提示',
      message: '请先进行登录再进行浏览页面~~~',
      type: 'warning'
    })
  } else {
    // 用户已登录，允许导航
    next()
  }
})

export default router
